package com.xiaochong.tir.apiservice.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.ExchangeTransactionpairMiddleMapper;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.util.CollectionUtil;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @author cx
 * @time 2018-05-30 19:57
 * 
 */
@Service
public class ExchangeTransactionpairMiddleService {
 
      
    @Autowired
	private ExchangeTransactionpairMiddleMapper exchangeTransactionpairMiddleDao;

    @Autowired
    private RedisServer redisServer;
	
	/**
     * 新增
     * @param entity 
     * @param session
     */
    public int insertSelective (ExchangeTransactionpairMiddle entity) { 
     return this.exchangeTransactionpairMiddleDao.insertSelective(entity);
    }	
    
    /**
     * 批量新增
     * @param list 
     * @param session
     */
    public void insertBatch(List<ExchangeTransactionpairMiddle> list) {
           
          this.exchangeTransactionpairMiddleDao.insertBatch(list);
         
         
    }
    
    /**
     * 根据主键修改单条数据
     * @param entity 
     * @param session
     */
    public int updateByPrimaryKey(ExchangeTransactionpairMiddle entity) {
           return this.exchangeTransactionpairMiddleDao.updateByPrimaryKey(entity);
    }
    
    /**
     * 根据主键批量修改数据
     * @param list 
     * @param session
     */
    public void updateBatch(List<ExchangeTransactionpairMiddle> list) {
           
          this.exchangeTransactionpairMiddleDao.updateBatch(list);
          
        
    }
    
    /**
     * 根据主键查询单条数据
     * @param id
     */
    public ExchangeTransactionpairMiddle selectByPrimaryKey(Integer id) {
            return this.exchangeTransactionpairMiddleDao.selectByPrimaryKey(id);
    }
    
    /**
     * 根据条件查询单条记录
     * @param entity 
     */
    public ExchangeTransactionpairMiddle selectByEntity(ExchangeTransactionpairMiddle entity) {
           return this.exchangeTransactionpairMiddleDao.selectByEntity(entity);
    }
    
    
    /**
     * 根据条件查询多条记录
     * @param entity 
     */
    public List<ExchangeTransactionpairMiddle> selectByEntityList(ExchangeTransactionpairMiddle entity) {
           return  this.exchangeTransactionpairMiddleDao.selectByEntityList(entity);
    }
    
    /**
     * 根据条件查询Id
     * @param entity 
     */
    public Integer selectById(ExchangeTransactionpairMiddle entity) {
           return  this.exchangeTransactionpairMiddleDao.selectById(entity);
    }
    
     /**
     * 根据条件查询Ids
     * @param entity 
     */
    public List<Integer> selectByIds(ExchangeTransactionpairMiddle entity) {
        return  this.exchangeTransactionpairMiddleDao.selectByIds(entity);
    }
    
     /**
     * 根据主键删除单条记录
     * @param id
     */
    public void deleteByPrimaryKey(Integer id) {
           this.exchangeTransactionpairMiddleDao.deleteByPrimaryKey(id);
    }
    
    /**
     * 根据主键删除多条记录
     * @param list
     */
    public void deleteBatch(List<Integer> list) {
           this.exchangeTransactionpairMiddleDao.deleteBatch(list);
    }
    
     /**
     * 根据某些条件删除
     * @param entity
     */
    public void deleteBatch(ExchangeTransactionpairMiddle entity) {
           this.exchangeTransactionpairMiddleDao.deleteByEntity(entity);
    }


    /**
     *交易对定时任务
     */
    @Transactional(rollbackFor = Exception.class)
    public void toEnable() {
        LogTrace.beginTrace();
        String data = redisServer.get(RedisKeysUtils.EXCHANGE_INFO);
        LogTrace.info("ExchangeTransactionpairMiddleService.toEnable","data",data);
        if(StringUtils.isBlank(data)){
            return;
        }
        JSONObject jsonObject = JSON.parseObject(data);
        if(CollectionUtil.isBlank(jsonObject)){
            return;
        }
        jsonObject = jsonObject.getJSONObject(RedisKeysUtils.EXCHANGE);
        if(CollectionUtil.isBlank(jsonObject)){
            return;
        }
        ExchangeTransactionpairMiddle search = new ExchangeTransactionpairMiddle();
        List<ExchangeTransactionpairMiddle> exchangeTransactionpairMiddles
                = this.exchangeTransactionpairMiddleDao.selectByEntityList(search);
        if(CollectionUtil.isBlank(exchangeTransactionpairMiddles)){
            return;
        }
        for (ExchangeTransactionpairMiddle middle:exchangeTransactionpairMiddles) {
            JSONObject jsonObject1 = jsonObject.getJSONObject(middle.getExchangeId()+"");

            middle.setEnableFlag(false);
            if(CollectionUtil.isNotBlank(jsonObject1)){
                JSONArray symbols = jsonObject1.getJSONArray("symbols");
                if(CollectionUtil.isNotBlank(symbols)){
                    for (int i = 0; i < symbols.size(); i++) {
                        String string = symbols.getString(i);
                        if(StringUtils.equals(string,middle.getTransactionName())){
                            middle.setEnableFlag(true);
                        }
                    }
                }
            }

            middle.setUpdateTime(new Date());
            exchangeTransactionpairMiddleDao.updateByPrimaryKey(middle);
        }
        LogTrace.endTrace();
    }
}